#include "util.h"
#include "DynTargetManagerPrivate.h"
#include "DynTargetItemSetPrivate.h"

#include <qdebug>

DynTargetManagerPrivate::DynTargetManagerPrivate(QObject* parent):
QObject(parent)
{
}

DynTargetManagerPrivate::~DynTargetManagerPrivate()
{
}

bool DynTargetManagerPrivate::initialize(const QString& config)
{
	auto buffer = readAllBufferFromFile(config);
	if(buffer.isEmpty())
		return false;

	QString error;

	QDomDocument doc;
	if(!doc.setContent(buffer,false,&error))
		return false;

	QDomElement root = doc.documentElement();
	if(root.tagName() != "config")
		return false;

	auto node = root.firstChild();
	while(!node.isNull())
	{
		auto name = node.nodeName();
		if(name == "color")
			parseColorNode(node);
		else if(name == "icon")
		{
			auto leaf = node.firstChildElement("civil");
			parseTargetConfigNode(leaf,civilTargetMapper);
			leaf = node.firstChildElement("military");
			parseTargetConfigNode(leaf,militaryTargetMapper);
		}
		node = node.nextSibling();
	}

	return true;
}

void DynTargetManagerPrivate::parseColorNode(const QDomNode& node)
{
	auto leaf = node.firstChild();
	while(!leaf.isNull())
	{
		auto name = leaf.nodeName();
		if(name == "colorItem")
		{
			auto nation = leaf.firstChildElement("nation").toElement().text();
			auto color = leaf.firstChildElement("color").toElement().text();
			nationColorMapper[nation] = color;
		}
		leaf = leaf.nextSibling();
	}
}

void DynTargetManagerPrivate::parseTargetConfigNode(const QDomNode& node,QMap<QString,TargetUnit>& mapper)
{
	auto leaf = node.firstChild();
	while(!leaf.isNull())
	{
		auto name = leaf.nodeName();
		if(name == "item")
		{
			auto name = leaf.firstChildElement("name").toElement().text();
			auto file = leaf.firstChildElement("file").toElement().text();
			auto size = leaf.firstChildElement("size").toElement().text();
			auto scale = leaf.firstChildElement("scale").toElement().text().toDouble();
			
			QStringList items = size.split(',');
			if(items.size() >= 2 && scale > 0)
			{
				int width = items.at(0).toInt();
				int height = items.at(1).toInt();
				TargetUnit unit;
				unit.name = name;
				unit.icon = file;
				unit.width = width;
				unit.height = height;
				unit.scale = scale;
				mapper.insert(name,unit);
			}
		}
		leaf = leaf.nextSibling();
	}
}

DynTargetItemSet* DynTargetManagerPrivate::createDynTargetItemSet(const QString& name)
{
	if(targetItemSet.contains(name))
		return nullptr;

	auto set = new DynTargetItemSetPrivate(name,this);
	targetItemSet.insert(name,set);
	return set;
}

bool DynTargetManagerPrivate::containsDynTargetItemSet(const QString& name)
{
	return targetItemSet.contains(name);
}

QStringList DynTargetManagerPrivate::getDynTargetItemSetList()const
{
	return targetItemSet.keys();
}

bool DynTargetManagerPrivate::removeTargetItemSet(DynTargetItemSet* set)
{
	if(!set)
		return false;

	auto itr = targetItemSet.begin();
	while(itr != targetItemSet.end())
	{
		if(itr.value() == set)
		{
			targetItemSet.remove(itr.key());
			return true;
		}
		itr++;
	}
	return false;
}

bool DynTargetManagerPrivate::defineDynamicTargetType(int code,const QString& nation,const QString& description,const QString& icon)
{
	return false;
}